/**
 * 
 */
package co.fitstart.admin.dao;

import java.util.List;

import co.fitstart.entity.content.Article;
import co.fitstart.entity.content.ArticleModule;
import co.fitstart.entity.content.Module;

import com.demo2do.core.persistence.annotation.Dynamic;
import com.demo2do.core.persistence.annotation.Execute;
import com.demo2do.core.persistence.annotation.Limit;
import com.demo2do.core.persistence.annotation.Query;
import com.demo2do.core.persistence.annotation.Variable;

/**
 * @author downpour
 *
 */
@Dynamic("hibernate")
public interface ArticleDao {
    
    /**
     * list article modules by page
     * 
     * @param moduleId
     * @param beginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM ArticleModule WHERE module.id = :moduleId ORDER BY serial DESC")
    public List<ArticleModule> list(@Variable("moduleId") Long moduleId, @Variable("beginIndex") int beginIndex, @Variable("maxResult") int maxResult);

    /**
     * list sticky article modules
     * 
     * @param module
     * @return
     */
    @Query("FROM ArticleModule articleModule WHERE articleModule.serial > 100 AND articleModule.module = :module ORDER BY articleModule.serial ASC")
    public List<ArticleModule> list(@Variable("module") Module module);
    
    /**
     * 
     * @param module
     * @param article
     * @return
     */
    @Query("FROM ArticleModule WHERE article = :article AND module = :module")
    public ArticleModule load(@Variable("module") Module module, @Variable("article") Article article);
    
    /**
     * 
     * @param article
     */
    @Execute("DELETE FROM ArticleModule WHERE article = :article")
    public void deleteArticleModules(@Variable("article") Article article);
    
    /**
     * 
     * @param article
     * @param moduleIdsToDelete
     */
    @Execute("DELETE FROM ArticleModule articleModule WHERE articleModule.article = :article AND articleModule.module.id in (:moduleIds)")
    public void deleteArticleModules(@Variable("article") Article article, @Variable("moduleIds") List<Long> moduleIdsToDelete);
    
    
}
